<?php

namespace App\Console\Commands;

use App\Models\Lottery\Lottery;
use App\Models\Lottery\LotteryStatistics;
use App\Models\Order\Order;
use App\Models\User\Record;
use App\Models\User\User;
use Illuminate\Console\Command;
use App\Models\User\DAU;
use Illuminate\Support\Facades\DB;

class DayStatistics extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:day-statistics';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $date = date("Y-m-d", time() - 86400);
        $lotteryStatistics = LotteryStatistics::find($date);

        if (!$lotteryStatistics) {

            $num200 = Order::select(DB::raw("SUM(num) AS num"))->where("test", 0)->where("price", 200)->whereBetween("open_time", [$date, date("Y-m-d")])->first()->num;

            $num100 = Order::select(DB::raw("SUM(num) AS num"))->where("test", 0)->where("price", 100)->whereBetween("open_time", [$date, date("Y-m-d")])->first()->num;

            $num50 = Order::select(DB::raw("SUM(num) AS num"))->where("test", 0)->where("price", 50)->whereBetween("open_time", [$date, date("Y-m-d")])->first()->num;

            $num20 = Order::select(DB::raw("SUM(num) AS num"))->where("test", 0)->where("price", 20)->whereBetween("open_time", [$date, date("Y-m-d")])->first()->num;

//            $amounts = Order::select(DB::raw("SUM(num*amounts) AS amounts"))->where("test", 0)->whereBetween("open_time", [$date, date("Y-m-d")])->first()->amounts;
//            $price = Order::select(DB::raw("SUM(num*price) AS price"))->where("test", 0)->where("win", 1)->whereBetween("open_time", [$date, date("Y-m-d")])->first()->price;

            $Lottery = Lottery::select(DB::raw("SUM(win) AS win, SUM(win2) AS win2, SUM(spend) AS spend"))->whereBetween("opentime", [$date, date("Y-m-d")])->first();

            $fee = Record::select(DB::raw("SUM(fee) AS fee"))->where("type", 5)->whereBetween("create_time", [$date, date("Y-m-d")])->first()->fee;

            $lotteryStatistics = new LotteryStatistics();
            $lotteryStatistics->date   = $date;
            $lotteryStatistics->num200 = (int)$num200;
            $lotteryStatistics->num100 = (int)$num100;
            $lotteryStatistics->num50  = (int)$num50;
            $lotteryStatistics->num20  = (int)$num20;
//            $lotteryStatistics->win    = (int)$amounts * 95 / 100 - (int)$price;
            $lotteryStatistics->spend  = (int)$Lottery->spend;
            $lotteryStatistics->win    = (int)$Lottery->win;
            $lotteryStatistics->win2   = (int)$Lottery->win2;
            $lotteryStatistics->fee    = (int)$fee;
            $lotteryStatistics->save();
        }
    }
}
